// ==UserScript==
// @name          Ikariam - City Database
// @version       1.0
// @date          2008-08-25
// @namespace     archeorbiter.googlecode.com
// @description   Version 1.0 (stand-alone) - Creates a table containing collected information on players and cities.
// @include       http://s*.ikariam.tld/*
// @include       http://s*.ikariam.com.pt/*
// @include       http://s*.ikariam.co.il/*
// @include       http://s*.fi.ikariam.com/*
// @resource W    wine.png
// @resource M    marble.png
// @resource C    crystal.png
// @resource S    sulphur.png
// @require       utility.js
// @require       help.js
// @require       store.js
// ==/UserScript==


css('#ot-3 { width: 980px; border-style: dotted; margin-bottom: 2em; }' +
    '#ot-3 th { font-size: 12px; font-weight: bold; text-align: center; border-bottom: double; background-color: #A8A92A; color: #542C0F; width: auto; }' +
    '#ot-3 .ot-city { border-left: double; }' +
    '#ot-3 p { line-height: 1; } #ot-3 p.t-1 { color: #336633; } #ot-3 p.t-2 { color: #666; }');


var view = document.getElementsByTagName('body')[0].id;
if (view == 'island') {
setAlly();
}

function setAlly() {
	function registerAlly(ul) {
		var cid = number($X('preceding-sibling::a[contains(@id,"city_")]', ul).id);
		var player = config.getCity("o", null, cid);
		var li = $X('li[@class="ally"]/span/following::text()[1]', ul);
		var ally = trim(li.textContent);
		config.setServer(["players", player, "a"], ally);
		
		var link = $X('li[@class="ally"]/a[@title="Send Message"]', ul);
		if (link) {
		var allyId= urlParse("allyId", link.href);
		config.setServer(["players", player, "aid"], allyId);
		}
	}
	var c = $x('id("cities")/li[contains(@class,"level") and not(contains(@class,"level0"))]/ul[@class="cityinfo"]');
	c.forEach(registerAlly);	
}


function players() {
	var table = <table id="ot-3" class="ot" align="center" border="1"><tr>
		<th id="ot-phead">Player</th>
		<th id="ot-ahead">Ally</th>
	</tr></table>;

	var playerNames = [];
	var players = config.getServer("players");
	for (var player in players) {
		playerNames[playerNames.length] = player;
	}
	playerNames.sort(alpha);
	
	var cityNumbers = [];
	for (var i = 0; i < playerNames.length; i++) {
		var pname = playerNames[i];
		var cities = config.getServer(["players", pname, "c"], {});
		cityNumbers[cityNumbers.length] = cities.length;
	}
	var headCount = Math.max.apply(Math, cityNumbers);

	for (var i = 0; i < headCount; i++) {
		var head = <th class="ot-city" colspan="3">City {i+1}</th>;
		table.tr.th += head;
	}

	for (var i = 0; i < playerNames.length; i++) {
		var pname = playerNames[i];
		var ally = config.getServer(["players", pname, "a"]);
		var cities = config.getServer(["players", pname, "c"], {});
		var cid = cities[0];
		var aid = config.getServer(["players", pname, "aid"]);	
		var row = <tr class="ot-row">
			<td><a href={urlTo("message", cid)}>{ pname }</a></td>
		</tr>;

		if (ally == "-") {
		var acell = <>
			<td>{ ally }</td>
		</>;
		row.td += acell;
		} else {
		var acell = <>
			<td><a href={ url("?view=allyPage&allyId="+ aid) }>{ ally }</a></td>
		</>;
		row.td += acell;
		}

		for (var j = 0; j < cities.length; j++) {
			var cname = config.getCity("n", null, cities[j]);
			var level = config.getCity(["l", buildingIDs.townHall], "", cities[j]);
			var status = config.getCity("z", null, cities[j]);
			var indicator = { 0: "", 1: "(v)", 2: "(i)" };
			var iid = config.getCity("i", null, cities[j]);
			var isle = config.getIsle([], null, iid);
			var iname = isle.x +":"+ isle.y;
			var iurl = urlTo("island", { island: iid, city: cities[j] });
			var res = isle.r;
			var cells = <>
				<td class="ot-city"><p class={ "t-"+ status }>{ cname } { indicator[status] }</p></td>
				<td>{ level }</td>
				<td><a title={ iname } href={ iurl }><img height="11" width="14" src={ GM_getResourceURL(res) }/></a></td>
			</>;
			row.td += cells;
		}
		
		if (cities.length < headCount) {
			for (var k = 0; k < headCount-cities.length; k++) {
				var dummy = <>
					<td class="ot-city"></td>
					<td></td>
					<td></td>
				</>;
				row.td += dummy;
			}
		}
		
		table.tr += row;
	}

	node({ append: document.body, tag: table });
	
	function sortByName() {
  sort(1);
	}
	function sortByAlly() {
	  sort(2);
	}
	var pth = $("ot-phead");
	clickTo(pth, sortByName);
	var ath = $("ot-ahead");
	clickTo(ath, sortByAlly);
}


var toggler = <input id="ot-3-toggle" class="button" type="button" value="Toggle city database"/>
node({ append: document.body, tag: toggler });
$("ot-3-toggle").addEventListener('click', toggleTable, false);

function toggleTable() {
	if (GM_getValue(location.host +'.toggle', false) == false) {
		GM_setValue(location.host +'.toggle', true);
		players();
	} else {
		GM_setValue(location.host +'.toggle', false);
		document.body.removeChild($("ot-3"));
	}
}

if (GM_getValue(location.host +'.toggle') == true) players();
